package org.lql.algo.codecrush.hot100.binaryTree;

import org.lql.algo.common.TreeNode;

/**
 * @author: liangqinglong
 * @date: 2025-09-07 17:47
 * @description: 101. 对称二叉树 <a href="https://leetcode.cn/problems/symmetric-tree/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class IsSymmetric {

	public boolean isSymmetric(TreeNode root) {
		// 增加空树判断，空树也认为是对称的，符合题目要求
		if (root == null) {
			return true;
		}
		return isMirror(root.left, root.right);
	}

	private boolean isMirror(TreeNode node1, TreeNode node2) {
		// 如果两个节点都为空，说明它们是镜像对称的
		if (node1 == null && node2 == null) {
			return true;
		}
		if (node1 == null || node2 == null || node1.val != node2.val) {
			return false;
		}
		return isMirror(node1.left, node2.right) && isMirror(node1.right, node2.left);
	}
}
